package com.feiyu.auth.granter;

import com.feiyu.user.entity.UserInfo;
import com.feiyu.user.feign.UserFeignClient;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.utils.Func;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 密码授权
 * @author tang ming
 * @since 2021/11/30 9:35
 */
@Component
@AllArgsConstructor
public class PasswordTokenGranter implements TokenGranter {

    public static final String GRANT_TYPE = "password";

    private UserFeignClient userFeignClient;

    private PasswordEncoder passwordEncoder;

    @Override
    public UserInfo grant(TokenParameter tokenParameter) {
        //获取参数中用户登录信息
        String account = tokenParameter.getStr("account");
        String password = tokenParameter.getStr("password");
        UserInfo userInfo = null;
        //、参数不为空 进行验证流程
        if(Func.isNoneBlank(account,password)){
            // 从用户服务获取登录的用户信息
            UserInfo result=userFeignClient.userInfo(account).getData();
            //验证密码正确性
            if (result!=null&&passwordEncoder.matches(password,result.getPassword()))
                userInfo=result;
        }
        return userInfo;
    }
}

